import { userList, getCount } from '../../../request/api'
const state = {
    userlist: [],
    pages: 1,
    size: 2,
    count: 0
}
const getters = {
    getUser(state) {
        return state.userlist
    },
    getCount(state) {
        return state.count
    }
}
const mutations = {
    REQ_LIST(state, payload) {
        state.userlist = payload
    },
    REQ_COUNT(state, payload) {
        state.count = payload
    },
    CHANGE_PAGE(state, payload) {
        state.pages = payload
    }

}
const actions = {
    //列表行动
    getUserActions(context) {
        console.log(context, '111111');
        userList({
            page: context.state.pages,
            size: context.state.size
        }).then(res => {
            if (res.code == 200) {
                console.log(res, '123123');
                let list = res.list ? res.list : [];
                if (context.state.pages != 1 && list.length == 0) {
                    context.dispatch("changePageAction", context.state.page - 1);
                }
                context.commit("REQ_LIST", res.list);
            }
        })

    },
    //获取总条数行动
    getCountActions({ commit }) {
        getCount().then(res => {
            if (res.code == 200) {
                commit("REQ_COUNT", res.list[0].total);
            }
        })
    },
    changePageAction(context, payload) {
        console.log(payload);
        context.commit('CHANGE_PAGE', payload);
        context.dispatch('getUserActions')
    }

}
export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}